//
//  VistorView.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/19.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit
import SnapKit


class VistorView: UIView
{
    
    var LoginRegClosure:(() -> ())?
    
    
    //需要添加的控件(设置其为私有属性+懒加载控件)
    //(1)圆圈图片
    private lazy var feedImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //(2)遮罩图片
    private lazy var maskImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //(3)房子图片
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //(4)文字
    private lazy var wordLabel:UILabel = {
        
        let lab:UILabel = UILabel()
        
        lab.font = UIFont.systemFontOfSize(14)
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.textColor = UIColor.grayColor()
        
        return lab
    }()
    
    //(5)登录按钮
    private lazy var loginButton:UIButton = {
        
        let button:UIButton = UIButton()
        
        button.addTarget(self, action: #selector(VistorView.LoginReg), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)

        return button
        
    }()
    
    //(6)注册按钮
    private lazy var regButton:UIButton = {
        
        let button:UIButton = UIButton()
        
        button.addTarget(self, action: #selector(VistorView.LoginReg), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return button
        
    }()
    

    //代码创建界面的入口
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局界面
    func setUpUI()
    {
        //(1)加圆圈图片
        //加不加都是同样的效果
        //self.feedImage.sizeToFit()
        //添加图片到主界面上
        addSubview(self.feedImage)
        
        //设置圆圈图片的约束
        self.feedImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        
        
        
        
        
        //(2)遮罩图片
        addSubview(self.maskImage)
        
        //设置遮罩图片的约束
        self.maskImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        
        
        
        //(3)房子图片
        addSubview(self.iconImage)
        
        //设置房子图片的约束
        self.iconImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //(4)文字
        addSubview(self.wordLabel)
        
        //设置文字的约束
        self.wordLabel.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.top.equalTo(maskImage.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        
        //(5)登录按钮
       addSubview(self.loginButton)
        
        self.loginButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.top.equalTo(wordLabel.snp_bottom).offset(16)
            make.leading.equalTo(wordLabel)
        }
        
        //(6)注册按钮
        addSubview(self.regButton)
        
        self.regButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.top.equalTo(wordLabel.snp_bottom).offset(16)
            make.trailing.equalTo(wordLabel)
        }
        
    }
    
    //圆圈图片旋转
    func feedImageRotation()
    {
        //(1)创建基本动画对象
        let anim:CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        
        //(2)设置动画属性
        //旋转角度
        anim.toValue = M_PI * 2
        
        //动画的无限性
        anim.repeatCount = MAXFLOAT
        
        //总持续时间
        anim.duration = 10
        
        //匀速运动
        anim.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
        
        // 当切换控制器或者程序退到后台 默认动画会停止
        anim.removedOnCompletion = false
        
        //加载到圆圈图片上
        self.feedImage.layer.addAnimation(anim, forKey: nil)
        
    }
    
    //外接一个方法，用于根据不同的controller设置不同的界面
    //参数必须设置可选值，用于辨别出是否是Home界面的
    func setUpUIWithVCInfo(imagStr:String?,wordStr:String?)
    {
        //判断传入的参数是否有值
        if let imag = imagStr,word = wordStr
        {
            self.iconImage.image = UIImage(named: imag)
            
            self.wordLabel.text = word
            
            self.feedImage.hidden = true
        }
        //首页时
        else
        {
            //圆圈图片转动
            self.feedImageRotation()
        }
    }
    
    //登录或注册按钮的触发事件
    //当方法是private私有事件时,前面必须加@objc转换成运行时系统方法
    @objc private func LoginReg()
    {
        LoginRegClosure?()
    }
    

}
